package com.jiuli.liping.service.impl;

import com.jiuli.liping.dao.CommodityDao;
import com.jiuli.liping.dao.ProductDao;
import com.jiuli.liping.dao.ProductOrderDao;
import com.jiuli.liping.dao.ShoppingCartDao;
import com.jiuli.liping.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jiuli.liping.service.ShoppingCartService;

import java.util.List;

/**
 * Author zyl
 * Date  2020-03-28
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartDao shoppingCartDao;

    @Autowired
    private ProductDao productDao;

    @Autowired
    private ProductOrderDao productOrderDao;

    @Autowired
    private CommodityDao commodityDao;
    
    @Override
    public ShoppingCart get(String id){
        return shoppingCartDao.get(id);
    }
    
    @Override
    public List<ShoppingCart> findList(ShoppingCart shoppingCart) {
        return shoppingCartDao.findList(shoppingCart);
    }
    
    @Override
    public List<ShoppingCart> findAllList() {
        return shoppingCartDao.findAllList();
    }
    
    @Override
    public Message insert(ShoppingCart shoppingCart) {
        Message me = new Message();

        //当前库存
        Commodity commodity = new Commodity();
        commodity.setCommodityNumber(shoppingCart.getTitleNumber());
        //找到该商品的图片价格名字
        Commodity commodity1 = commodityDao.selectOneCommodity(commodity);
        String amount = productDao.findSumAmount(commodity.getCommodityNumber());
        String realAmount = productOrderDao.findSumRealAmount(commodity.getCommodityNumber());
        if(amount == null){ amount="0";}if(realAmount == null){ realAmount="0";}
        int fbk2 = Integer.parseInt(amount)-Integer.parseInt(commodity1.getFbk3())-Integer.parseInt(realAmount);

        List<Photograph> list1 = commodityDao.findAllListByPhotograph(commodity.getCommodityNumber());
        int a=0;
        if(fbk2 >= Integer.parseInt(shoppingCart.getNumber())){
            shoppingCart.setImage(list1.get(0).getPhotographUrl());
            shoppingCart.setPrice(commodity1.getSalesPrice());
            shoppingCart.setTitle(commodity1.getCommodityFatherName());
            ShoppingCart sc = shoppingCartDao.getCountByProductNumber(shoppingCart);
            if(sc!=null){//查看账号的商品是否存在
                shoppingCart.setNumber((Integer.parseInt(sc.getNumber())+Integer.parseInt(shoppingCart.getNumber()))+"");
                a = shoppingCartDao.updateNumberByUserNum(shoppingCart);
            }else {
                //判断是不是食堂酒水专用，食堂专用不允许混合购买
                //先判断购物车有无食堂酒水
                int bb = shoppingCartDao.getCountSt(shoppingCart);
                if(bb>0){
                    //有酒水,新增的一定是酒水的
                    if("1719".equals(shoppingCart.getTitleNumber().substring(0,4))){
                        a = shoppingCartDao.insert(shoppingCart);
                    }else {
                        me.setFlag1("3");
                        me.setMessage("加入购物车失败，食堂商品和普通商品不能混合购买");
                        return me;
                    }
                }else{
                    //无酒水
                    //看新增的和购物车是否有区别
                    int bb1 = shoppingCartDao.getCountSt1(shoppingCart);
                    if(bb1>0){
                        if(!"1719".equals(shoppingCart.getTitleNumber().substring(0,4))){
                            a = shoppingCartDao.insert(shoppingCart);
                        }else {
                            me.setFlag1("3");
                            me.setMessage("加入购物车失败，食堂商品和普通商品不能混合购买");
                            return me;
                        }
                    }else{
                        a = shoppingCartDao.insert(shoppingCart);
                    }
                }
            }
        }else{
            a = 2;
        }
        List<ShoppingCart> list = shoppingCartDao.findAllCartListByNumber(shoppingCart);
        if(a==1){
            me.setFlag1("1");
            me.setMessage("success");
        }else{
            me.setFlag1("2");
            me.setMessage("failed");
        }
        if(list !=null){
            me.setCartNumber(list.size());
        }
        return me;
    }
    
    @Override
    public int insertBatch(List<ShoppingCart> shoppingCarts){
        return shoppingCartDao.insertBatch(shoppingCarts);
    }
    
    @Override
    public int update(ShoppingCart shoppingCart) {
        return shoppingCartDao.update(shoppingCart);
    }
    
    @Override
    public int delete(ShoppingCart shoppingCart) {
        return shoppingCartDao.delete(shoppingCart);
    }

    @Override
    public List<ShoppingCart> findAllCartListByNumber(ShoppingCart shoppingCart) {
        List<ShoppingCart> list = shoppingCartDao.findAllCartListByNumber(shoppingCart);
        for (ShoppingCart sh: list) {
            sh.setNumberToInt(Integer.parseInt(sh.getNumber()));

            //商品现有库存
            Commodity commodity = new Commodity();
            commodity.setCommodityNumber(sh.getTitleNumber());
            Commodity commodity2 = commodityDao.selectOneCommodity(commodity);
            //属性拼接
            CommodityServiceImpl co = new CommodityServiceImpl();
            String spsx = co.getPropertyNameTo(commodity2);
            sh.setSpsx(spsx);

            String amount = productDao.findSumAmount(sh.getTitleNumber());
            String realAmount = productOrderDao.findSumRealAmount(sh.getTitleNumber());
            if(amount == null){ amount="0";}if(realAmount == null){ realAmount="0";}
            int fbk2 = Integer.parseInt(amount)-Integer.parseInt(commodity2.getFbk3())-Integer.parseInt(realAmount);
            if("0".equals(amount)){//0代表商品已经下架
                sh.setStockToInt(Integer.parseInt(sh.getNumber()));
            }else {
                sh.setStockToInt(fbk2);
            }
        }
        return list;
    }

    @Override
    public int updateNumber(ShoppingCart shoppingCart) {
        //商品现有库存
        Commodity commodity = new Commodity();
        commodity.setCommodityNumber(shoppingCart.getTitleNumber());
        Commodity commodity2 = commodityDao.selectOneCommodity(commodity);
        String amount = productDao.findSumAmount(shoppingCart.getTitleNumber());
        String realAmount = productOrderDao.findSumRealAmount(shoppingCart.getTitleNumber());
        if(amount == null){ amount="0";}if(realAmount == null){ realAmount="0";}
        int fbk2 = Integer.parseInt(amount)-Integer.parseInt(commodity2.getFbk3())-Integer.parseInt(realAmount);

        int a =0 ;
        if(fbk2 >= Integer.parseInt(shoppingCart.getNumber())){
            a = shoppingCartDao.updateNumber(shoppingCart);
        }else{
            a = 2;
        }
        return a;
    }

    @Override
    public int deleteAll(ShoppingCart shoppingCart) {
        return shoppingCartDao.deleteAll(shoppingCart);
    }

}
